package com.zhongxi.guoyuan.fragment.RewardCoin;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.CommentAdapter;
import com.zhongxi.guoyuan.adapter.RewardCommentAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.comment.CommentData;
import com.zhongxi.guoyuan.entity.comment.CommentResult;
import com.zhongxi.guoyuan.entity.rewardcoin.MallCommentResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;
import com.zhongxi.guoyuan.widget.xlistview.XListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class RewardCoinCommentFragment extends BaseFragment {

    private String goodsid;
    private XListView lvList;
    private List<CommentData> data = new ArrayList<>();
    private CommentAdapter adapter;
    private boolean isMore;

    public RewardCoinCommentFragment(String goodsid) {
        this.goodsid = goodsid;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_reward_coin_comment, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        lvList = (XListView) getView().findViewById(R.id.lvList);
        lvList.setPullLoadEnable(true);
        lvList.setPullRefreshEnable(true);

    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new CommentAdapter(context, data);
        lvList.setAdapter(adapter);
        rewardComments();
    }

    private void rewardComments() {
        String str = "&gid=" + goodsid + "&page=" + pageNum;
        getServer(Url.MALL_COMMENTS, null, str, false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        lvList.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                isMore = false;
                pageNum = 1;
                rewardComments();
            }

            @Override
            public void onLoadMore() {
                isMore = true;
                pageNum++;
                rewardComments();
            }
        });
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            CommentResult result = gson.fromJson(response, CommentResult.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());
            if (isMore) {
                if (result.getComments() == null || result.getComments().size() == 0) {
                    showShortToast("没有加载更多了");
                    isFinish();
                    return;
                }
            }
            if (pageNum == 1) {
                data.clear();
            }
            if (result.getComments().size() > 0) {
                data.addAll(result.getComments());
            }
            JSONObject obj = new JSONObject(response);
            JSONObject userObj = obj.getJSONObject("user");
            if (null != userObj && !"".equals(userObj)) {
                adapter.setUser(userObj);
            }
            adapter.notifyDataSetChanged();
            isFinish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void isFinish() {
        lvList.stopRefresh();
        lvList.stopLoadMore();
        lvList.setRefreshTime("刚刚");
    }
}
